package com.cx.Controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.cx.entity.Article;
import com.cx.entity.User;
import com.cx.service.IUserService;
import com.cx.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cx
 * @since 2024-06-07
 */
@CrossOrigin    //解决跨域问题注解
@RestController //返回UserJson格式数据接口
@RequestMapping("user")
public class UserController {

    //配置基于权重的轮询算法
    @Reference(loadbalance = "roundrobin")
    private IUserService userService;

    @RequestMapping("login")
    public R loginById(@RequestParam("username") String username,
                       @RequestParam("password") String password,
                       HttpSession session) {
//        转化为Map格式传递参数
        Map<String, Object> person = new HashMap<String, Object>();
        person.put("username", username);
        person.put("password", password);
//            判断用户登录用户的rid是否等于2
        person.put("rid", 2);
        session.setAttribute("userHome", username);
//        System.out.println("person = " + person);
        return userService.userLogin(person);
    }

    //    添加用户
    @RequestMapping("save")
    public R userSave(@RequestParam("username") String username,
                      @RequestParam("password") String password,
                      @RequestParam("phone") String phone) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhone(phone);
        return R.ok().put("data", userService.save(user));
    }

    //    修改用户
    @RequestMapping("Update")
    public R userUpdate(User user) {
        return R.ok().put("data", userService.updateById(user));
    }

    //    删除用户
    @RequestMapping("del")
    public R delUser(@PathParam("id") Integer id) {
        return R.ok().put("data", userService.removeById(id));
    }

    //    查询全部用户信息
    @RequestMapping("list")
    public R getUserAll() {
        return R.ok().put("data", userService.list());
    }


    //==================================================
    //    查询用户余额  地址栏后面直接写id，不需要“？id=1”
    @RequestMapping("money/{id}")
    public R getUserMoneyById(@PathVariable("id") Integer id) {
//        System.out.println("id = " + id);
        return R.ok().put("data", userService.getById(id).getMoney());
    }

    //    余额修改
    @RequestMapping("money")
    public R UserMoneyUpdate(@PathParam("id") Integer id, @PathParam("money") Float money) {
        User user = new User();
        user.setId(id);
        user.setMoney((userService.getById(id).getMoney()) + (float)money);
//        System.out.println("id = " + id);
//        System.out.println("Money = " + money);
        return R.ok().put("state",userService.updateById(user)).put("data", user.getMoney());
    }

    //    用户打赏功能
    @RequestMapping("RewardMoney")
    public R UserRewardMoneyUpdate(@PathParam("id") Integer id,@PathParam("pId") Integer pId) {
        // 验证用户是否存在
        System.out.println("id = " + id);
        System.out.println("pId = " + pId);
        User user;
        try {
            user = userService.getById(id);
            userService.getById(pId);
        } catch (Exception e) {
            return R.error();
        }
        float money = 10;
        if (user.getMoney()-money>=0) {
            System.out.println(" 尝试打赏");
            return R.ok(0, "打赏对方成功").put("data",userService.RewardMoneyUpdate(id, pId, money));
        }
        return R.error(500,"打赏出现未知错误");

    }

    //==================================================

}
